package tools;

import funcktion.BookManage;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

public class BookUpdateGUI extends JFrame {
    private JTextField bookIdTextField;
    private JTextField bookNameTextField;
    private JTextField priceTextField;
    private JTextField totalCopiesTextField;
    private JTextField availableCopiesTextField;
    private JTextField statusTextField;
    private JButton updateButton;

    public BookUpdateGUI(String bookId) {
        setTitle("修改图书");
        //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridLayout(7, 2, 10, 10));

        JLabel bookNameLabel = new JLabel("图书名称");
        bookNameTextField = new JTextField();
        JLabel priceLabel = new JLabel("价格");
        priceTextField = new JTextField();
        JLabel totalCopiesLabel = new JLabel("总库存");
        totalCopiesTextField = new JTextField();
        JLabel availableCopiesLabel = new JLabel("可借数量");
        availableCopiesTextField = new JTextField();
        JLabel statusLabel = new JLabel("状态");
        statusTextField = new JTextField();

        updateButton = new JButton("修改");
        updateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                updateBookInfo(bookId);
            }
        });

        add(bookNameLabel);
        add(bookNameTextField);
        add(priceLabel);
        add(priceTextField);
        add(totalCopiesLabel);
        add(totalCopiesTextField);
        add(availableCopiesLabel);
        add(availableCopiesTextField);
        add(statusLabel);
        add(statusTextField);
        add(new JLabel());
        add(updateButton);

        pack();  //
        setLocationRelativeTo(null);

        try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/library", "root", "lang520520");
            PreparedStatement stmt1 = conn.prepareStatement("SELECT * FROM Book WHERE bookId = ?")) {
            // 设置参数并执行操作
            stmt1.setString(1, bookId);
            ResultSet rs = stmt1.executeQuery();
            System.out.println(rs);

            if (rs.next()) {
                // 处理结果集的数据
                System.out.println(rs.getString("bookName"));
                // 将查询结果放入文本框
                bookNameTextField.setText(rs.getString("bookName"));
                priceTextField.setText(String.valueOf(rs.getDouble("price")));
                totalCopiesTextField.setText(String.valueOf(rs.getInt("totalCopies")));
                availableCopiesTextField.setText(String.valueOf(rs.getInt("availableCopies")));
                statusTextField.setText(rs.getString("status"));

            }


        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    private void updateBookInfo(String bookId) {

        String bookName = bookNameTextField.getText();
        double price = 0;
        int totalCopies = 0;
        int availableCopies = 0;
        String status = statusTextField.getText();

        if (bookName.isEmpty() || status.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请填写所有必填字段。\n价格，总库存，可借数量只能为数字");
        } else {
            try {
                price = Double.parseDouble(priceTextField.getText());
                totalCopies = Integer.parseInt(totalCopiesTextField.getText());
                availableCopies = Integer.parseInt(availableCopiesTextField.getText());

                // 保存图书信息到数据库



                // 检查必填字段是否为空
                try(Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/library", "root", "lang520520");
                    PreparedStatement stmt2 = conn.prepareStatement("UPDATE Book SET bookName = ?, price = ?, totalCopies = ?, availableCopies = ?, status = ? WHERE bookId = ?")) {

                    stmt2.setString(1, bookName);
                    stmt2.setDouble(2, price);
                    stmt2.setInt(3, totalCopies);
                    stmt2.setInt(4, availableCopies);
                    stmt2.setString(5, status);
                    stmt2.setString(6, bookId);
                    stmt2.executeUpdate();

                    JOptionPane.showMessageDialog(this, "图书信息修改成功");

                    BookManage GUI = new BookManage();
                    GUI.setVisible(true);
                    setVisible(false);

                } catch (SQLException e) {
                    JOptionPane.showMessageDialog(this, "修改失败：" + e.getMessage());
                    e.printStackTrace();
                }
            } catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(this, "请输入有效的数字");
                e.printStackTrace();
            }
        }

    }
}
